// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config/config";
import Material from "./Material";
import CutUpVegetables from "../cook/CutUpVegetables";
import ReadyNode from "./ReadyNode";
import Order from "../order/Order";
import Cooking from "../cook/Cooking";
import func from "../config/func";
import Cook from "../cook/Cook";
import GameOver from "../over/GameOver";
import AudioManage from "../manage/AudioManage";
import GameTools from "../gameTool/GameTools";
import Suspend from "./Suspend";
import Guide from "../guide/Guide";
import AdManage from "../manage/AdManage";
import CutTime from "../vivo/CutTime";
import TouchByMistake from "../touchByMistake/TouchByMistake";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainGame extends cc.Component {

    @property({ type: cc.Label, tooltip: "货币" })
    m_money: cc.Label = null;
    @property({ type: cc.Sprite, tooltip: "显示第几关节点" })
    m_checkPointNode: cc.Sprite = null;
    @property({ type: cc.Label, tooltip: "第几关" })
    m_checkPoint: cc.Label = null;
    @property({ type: cc.Node, tooltip: "传送带父节点" })
    m_delivery: cc.Node = null;
    @property({ type: cc.Node, tooltip: "开始点节点" })
    m_startNode: cc.Node = null;
    @property({ type: cc.Node, tooltip: "中间点节点" })
    m_middleNode: cc.Node = null;
    @property({ type: cc.Node, tooltip: "结束点节点" })
    m_endNode: cc.Node = null;
    @property({ type: cc.Node, tooltip: "材料父节点" })
    m_material: cc.Node = null;
    @property({ type: cc.Node, tooltip: "备菜和切菜父节点" })
    m_readyAndComplete: cc.Node = null;
    @property({ type: cc.Node, tooltip: "切菜节点" })
    m_cutNode: cc.Node = null;
    @property({ type: cc.Node, tooltip: "炒菜节点" })
    m_cooking: cc.Node = null;
    @property({ type: cc.Node, tooltip: "厨师数组" })
    m_cook: cc.Node[] = [];
    @property({ type: cc.Sprite, tooltip: "关卡进度" })
    m_checkpointFinish: cc.Sprite = null;
    @property({ type: cc.Label, tooltip: "关卡剩余时间" })
    m_time: cc.Label = null;

    n_orderNode: cc.Node = null;
    n_checkPoint: any = null;
    n_cutUpComplete: cc.Node[] = [];
    n_ready: cc.Node[] = [];
    n_cutUpCompleteIndex = 0;
    n_cookMoveSpeed: number = 100;
    n_orderReward: number = 0;
    n_cutMove: boolean = false;
    n_materialArr: any[] = [];
    n_timeNum: number = 0;
    n_gameTotal: number = 0;
    n_orderArr: any[] = [];
    n_ingredientNumber: number[] = [];
    n_view: cc.Node = null;

    n_vivoTime: number = 0;

    start() {
        AdManage.instance.init();
        this.n_orderNode = this.node.getChildByName('orderNode');
        this.n_view = cc.find("Canvas/view");
        this.init();
        if (config.guide) {
            this.createGuide();
        }
    }

    init(next: boolean = true) {
        config.openPopup = false;
        config.addSpeed = 1;
        config.qq_onTrial = false;
        config.qq_gameOver = false;
        if (config.gameType == "QQ") {
            if (config.sclY >= 2) {
                AdManage.instance.initQQcreateBlockAd("landscape");
            }
            if (config.qq_loadCfg && config.qq_loadCfg.mistouchOn == 1) {
                this.loadQQPopup(1);
            } else {
                config.qq_stopTime = false;
                setTimeout(() => {
                    func.creatrBanner();
                }, 500);
                this.schedule(() => {
                    func.destroyBanner();
                    setTimeout(() => {
                        func.creatrBanner();
                    }, 500);
                }, 10)
            }
        }
        this.n_checkPoint = config.dataJson.checkPoint[10000 + config.checkPoint];
        if (next) {
            this.createDelivery();
            this.createCompleteNode();
            this.setIngredientType();
        }
        this.n_timeNum = this.n_checkPoint.time;
        this.n_gameTotal = this.n_checkPoint.upgrade;
        config.gameOver = false;
        this.n_cutUpCompleteIndex = 0;
        this.n_orderReward = 0;
        this.n_cutMove = false;
        this.m_checkpointFinish.fillRange = 0;
        this.m_cutNode.children[1].children[0].active = false;
        this.m_time.string = func.changeTime(this.n_timeNum);
        this.m_checkPoint.string = `第${config.checkPoint}关`;
        for (let i = 0; i < this.m_cook.length; i++) {
            let item = this.m_cook[i];
            if (i % 2 == 0) {
                item.getComponent(Cook).init(config.cook_1);
            } else {
                item.getComponent(Cook).init(config.cook_2);
            }
        }
        this.m_cooking.getComponent(Cooking).setSampleAndFoodActive();
        this.m_checkPointNode.node.active = true;
        this.n_vivoTime = 0;
        cc.tween(this.m_checkPointNode.node)
            .delay(1)
            .to(0.5, { opacity: 0 })
            .call(() => {
                config.cutTime = 1;
                this.m_startNode.getComponent(cc.Animation).play();
                this.n_orderNode.getComponent(Order).init();
                this.m_checkPointNode.node.active = false;
                this.m_checkPointNode.node.opacity = 255;
                this.schedule(this.counTDown, 1);
            })
            .start()
    }
    /**
     * 创建引导界面
     */
    async createGuide() {
        let child: cc.Node = null;
        if (config.prefabArr[config.prefabPath.guide]) {
            child = cc.instantiate(config.prefabArr[config.prefabPath.guide]);
        } else {
            let prefab = await func.loadResouce(config.prefabPath.guide, cc.Prefab) as cc.Prefab;
            config.prefabArr[config.prefabPath.guide] = prefab;
            child = cc.instantiate(prefab);
        }
        child.parent = this.n_view;
        child.zIndex = 1;
        this.m_checkPointNode.node.zIndex = 2;
        child.getComponent(Guide).showGuide(0, 0, cc.v2(), false);
    }
    /**
     * 新手引导选择材料
     */
    guideSelectMaterial() {
        Guide.instance.showGuide(this.m_middleNode.width, this.m_middleNode.height, cc.v2(this.m_middleNode.x, this.m_middleNode.y), true);
    }
    /**
     * 生成传送带
     */
    async createDelivery() {
        let startArr = this.n_checkPoint.start.split(","),
            startX = parseInt(startArr[0]) + this.m_startNode.width / 2 - cc.winSize.width / 2,
            startY = cc.winSize.height / 2 - (parseInt(startArr[1])),
            startPos = cc.v2(startX, startY);
        this.m_startNode.setPosition(startPos);
        if (this.n_checkPoint.leftQuantity == 2) {
            this.m_startNode.scaleY = 0.9;
        } else {
            this.m_startNode.scaleY = 1;
        }

        let middleArr = this.n_checkPoint.middle.split(","),
            middleX = parseInt(middleArr[0]) + this.m_middleNode.width / 2 - cc.winSize.width / 2,
            middleY = cc.winSize.height / 2 - (parseInt(middleArr[1]) + this.m_middleNode.height / 2),
            middlePos = cc.v2(middleX, middleY);
        this.m_middleNode.setPosition(middlePos);
        config.transitionPosition = cc.v2(middlePos.x, middlePos.y + this.m_middleNode.height / 3);

        let endArr = this.n_checkPoint.end.split(","),
            endX = parseInt(endArr[0]) + this.m_endNode.width / 2 - cc.winSize.width / 2,
            endY = cc.winSize.height / 2 - (parseInt(endArr[1]) + this.m_endNode.height / 2),
            endPos = cc.v2(endX, endY);
        this.m_endNode.setPosition(endPos);
        config.endPosition = endPos;

        let leftXArr = this.n_checkPoint.leftX.split(","),
            leftYArr = this.n_checkPoint.leftY.split(",");
        let rightXArr = this.n_checkPoint.rightX.split(","),
            rightYArr = this.n_checkPoint.rightY.split(",");
        for (let i = 0; i < this.n_checkPoint.leftQuantity; i++) {
            let child: cc.Node = null;
            if (config.prefabArr[config.nodeToolPath.left]) {
                child = GameTools.gameTools.get(config.nodeToolPath.left);
            } else {
                let prefab = await func.loadResouce(config.nodeToolPath.left, cc.Prefab) as cc.Prefab;
                config.prefabArr[config.nodeToolPath.left] = prefab;
                child = cc.instantiate(prefab);
            }
            let left_numX = parseInt(leftXArr[i]) + child.width / 2 - cc.winSize.width / 2,
                left_numY = cc.winSize.height / 2 - (parseInt(leftYArr[i]) + child.height / 2);
            child.setPosition(cc.v2(left_numX, left_numY));
            child.parent = this.m_delivery;
            if (i == this.n_checkPoint.leftQuantity - 1) {
                config.startPosition = cc.v2(left_numX, left_numY + child.height / 3);
            }
        }
        for (let i = 0; i < this.n_checkPoint.rightQuantity; i++) {
            let child: cc.Node = null;
            if (config.prefabArr[config.nodeToolPath.right]) {
                child = GameTools.gameTools.get(config.nodeToolPath.right);
            } else {
                let prefab = await func.loadResouce(config.nodeToolPath.right, cc.Prefab) as cc.Prefab;
                config.prefabArr[config.nodeToolPath.right] = prefab;
                child = cc.instantiate(prefab);
            }
            let right_numX = parseInt(rightXArr[i]) + child.width / 2 - cc.winSize.width / 2,
                right_numY = cc.winSize.height / 2 - (parseInt(rightYArr[i]) + child.height / 2);
            child.setPosition(cc.v2(right_numX, right_numY));
            child.parent = this.m_delivery;
        }
    }
    /**
     * 生成切好材料的节点
     */
    async createCompleteNode() {
        let prepareDishesX = this.n_checkPoint.prepareDishesX.split(","),
            prepareDishesY = this.n_checkPoint.prepareDishesY.split(",");
        for (let i = 0; i < this.n_checkPoint.prepareDishesNumber; i++) {
            let child: cc.Node = null;
            if (config.prefabArr[config.nodeToolPath.ready]) {
                child = GameTools.gameTools.get(config.nodeToolPath.ready);
                child.getComponent(ReadyNode).state = "ready";
            } else {
                let prefab = await func.loadResouce(config.nodeToolPath.ready, cc.Prefab) as cc.Prefab;
                config.prefabArr[config.nodeToolPath.ready] = prefab;
                child = cc.instantiate(prefab);
            }
            if (child.children[1].childrenCount > 0) {
                for (let i = 0; i < child.children[1].childrenCount; i++) {
                    let item = child.children[1].children[i];
                    item.getComponent(Material).recoveryMaterial();
                    i--;
                }
            }
            child.parent = this.m_readyAndComplete;
            let numX = parseInt(prepareDishesX[i]) + child.width / 2 - cc.winSize.width / 2,
                numY = cc.winSize.height / 2 - (parseInt(prepareDishesY[i]) + child.height / 2);
            child.setPosition(cc.v2(numX, numY))
            this.n_ready.push(child);
        }
        let putVegetablesX = this.n_checkPoint.putVegetablesX.split(","),
            putVegetablesY = this.n_checkPoint.putVegetablesY.split(",");
        for (let i = 0; i < this.n_checkPoint.putVegetablesNumber; i++) {
            let child: cc.Node = null;
            if (config.prefabArr[config.nodeToolPath.complete]) {
                child = GameTools.gameTools.get(config.nodeToolPath.complete);
            } else {
                let prefab = await func.loadResouce(config.nodeToolPath.complete, cc.Prefab) as cc.Prefab;
                config.prefabArr[config.nodeToolPath.complete] = prefab;
                child = cc.instantiate(prefab);
            }
            child.children[2].active = false;
            child.parent = this.m_readyAndComplete;
            let numX = parseInt(putVegetablesX[i]) + child.width / 2 - cc.winSize.width / 2,
                numY = cc.winSize.height / 2 - (parseInt(putVegetablesY[i]) + child.height / 2);
            child.setPosition(cc.v2(numX, numY));
            this.n_cutUpComplete.push(child);
        }
    }
    /**
     * 生成材料
     */
    async startCreateMaterial() {
        let child: cc.Node = null;
        if (config.prefabArr[config.nodeToolPath.material]) {
            child = GameTools.gameTools.get(config.nodeToolPath.material);
        } else {
            let prefab = await func.loadResouce(config.nodeToolPath.material, cc.Prefab) as cc.Prefab;
            config.prefabArr[config.nodeToolPath.material] = prefab;
            child = cc.instantiate(prefab);
        }
        // if (!child) return;
        child.scale = 0;
        child.parent = this.m_material;
        child.setPosition(config.startPosition);
        let index = Math.floor(Math.random() * this.n_ingredientNumber.length);
        child.getComponent(Material).init(this.n_ingredientNumber[index]);
    }
    /**
     * 设置显示原材料
     */
    setIngredientType() {
        let orderTypeStr: string = this.n_checkPoint.number,
            orderTypeArr = orderTypeStr.split(",");
        for (const key in config.dataJson.vegetableType) {
            let item = config.dataJson.vegetableType[key];
            if (orderTypeArr.indexOf(item.number.toString()) != -1) {
                this.n_orderArr.push(item);
                let ingredientStr: string = item.mixedIngredients,
                    ingredientArr = ingredientStr.split(",");
                for (let j = 0; j < ingredientArr.length; j++) {
                    let id = parseInt(ingredientArr[j]);
                    if (this.n_ingredientNumber.indexOf(id) == -1) {
                        this.n_ingredientNumber.push(id);
                    }
                }
            }
        }
        // let ingredientStr: string = this.n_checkPoint.ingredientNumber,
        //     ingredientArr = ingredientStr.split(",");
        // for (let i = 0; i < ingredientArr.length; i++) {
        //     this.n_ingredientNumber.push(parseInt(ingredientArr[i]));
        // }
    }
    /**
     * 时间倒计时
     */
    counTDown() {
        if (config.qq_stopTime) return;
        if (config.gameType == "vivo" && config.cutTime == 1 && !config.guide) {
            this.n_vivoTime++;
            if (this.n_vivoTime == 30) {
                this.n_vivoTime = 0;
                this.loadVivoTime();
            }
        }
        this.n_timeNum--;
        this.m_time.string = func.changeTime(this.n_timeNum);
        if (this.n_timeNum <= 0) {
            this.gameOver(false);
        }
    }
    /**
     * 游戏结束
     * @param completeGame 是否完成游戏
     */
    gameOver(completeGame: boolean) {
        config.gameOver = true;
        this.unschedule(this.counTDown);
        // this.unschedule(this.startCreateMaterial);
        this.m_startNode.getComponent(cc.Animation).stop();
        let timeNode = this.n_view.getChildByName("timeNode");
        if (timeNode && timeNode.active) timeNode.getComponent(CutTime).close();
        this.loadSettlementInterface(completeGame, this.n_orderReward);
        if (config.gameType == "QQ" && completeGame && config.qq_loadCfg && config.qq_loadCfg.mistouchOn == 1) {
            this.loadQQPopup(3);
        }
    }
    /**
     * 每完成一个订单 增长的进度
     * @param checkpointFinish 
     */
    completeOrder(checkpointFinish: number) {
        this.m_checkpointFinish.fillRange += checkpointFinish / this.n_gameTotal;
        if (this.m_checkpointFinish.fillRange >= 1) {
            this.gameOver(true);
        }
        if (config.guide) {
            Guide.instance.showGuide(this.m_cook[0].width, this.m_cook[0].height, cc.v2(this.m_cook[1].x, this.m_cook[1].y), true);
        }
    }
    /**
     * 开始切菜
     * @param vegetables 蔬菜所在节点
     */
    startCutUpVegetables(vegetables: cc.Node) {
        if (!vegetables || vegetables.childrenCount <= 0 || this.m_cutNode.children[1].children[0].active || this.n_cutMove || config.gameOver) return;
        if (config.guide && config.orderArr.length == 1) {
            Guide.instance.showGuide(this.m_cutNode.width, this.m_cutNode.height, cc.v2(this.m_cutNode.x, this.m_cutNode.y), true);
        }
        vegetables.parent.getComponent(ReadyNode).state = "ready";
        let len = vegetables.childrenCount;
        this.n_cutMove = true;
        for (let i = 0; i < vegetables.childrenCount; i++) {
            len--;
            let child: cc.Node = vegetables.children[i],
                childWorldPos = vegetables.convertToWorldSpaceAR(cc.v2(child.x, child.y)),
                nodePos = cc.v2(childWorldPos.x - this.node.width / 2, childWorldPos.y - this.node.height / 2);
            child.parent = this.node;
            child.setPosition(nodePos);
            i--;
            cc.tween(child)
                .to(1, { x: this.m_cutNode.x, y: this.m_cutNode.y })
                .call(() => {
                    GameTools.gameTools.put(child, config.nodeToolPath.material);
                    if (len == 0) {
                        this.n_cutMove = false;
                        this.m_cutNode.children[1].getComponent(CutUpVegetables).init();
                    }
                })
                .start();
        }
    }
    /**
     * 切菜完成准备烹饪
     */
    cutUpVagetablesOver() {
        for (let i = 0; i < this.n_cutUpComplete.length; i++) {
            let child = this.n_cutUpComplete[i].children[2];

            if (!child.active) {
                child.active = true;
                return true;
            }
        }
        return false
    }
    /**
     * 继续切下一道菜
     */
    countineCutUpVagetables() {
        this.n_orderNode.getComponent(Order).cutUpVegetables();
    }
    /**
     * 检查厨师状态
     * @param type 1判断有没有空闲厨师  2判断厨师手中还有没有材料
     */
    checkCookState(type: number) {
        let cooking = this.m_cooking.getComponent(Cooking);
        let len = 0;
        if (config.guide) {
            len = 1;
        } else {
            len = this.m_cook.length;
        }
        if (type == 2) {
            for (let i = 0; i < len; i++) {
                let item = this.m_cook[i];
                if (item.active) {
                    if (item.children[0].active && !item.getComponent(Cook).n_move) {
                        let info = item.getComponent(Cook).n_info;
                        cooking.init(info);
                        item.children[0].active = false;
                        return;
                    }
                }
            }
        }
        let cook: cc.Node = null;
        for (let i = 0; i < len; i++) {
            let item = this.m_cook[i];
            if (item.active && !item.children[0].active && !item.getComponent(Cook).n_move) {
                cook = item;
                break;
            }
        }
        this.startCooking(cook, cooking);
    }
    /**
     * 开始烹饪
     */
    startCooking(cook: cc.Node, cooking: Cooking) {
        if (config.gameOver || !cook) return;
        let _cook = cook.getComponent(Cook),
            readyFood: cc.Node = null,
            len = this.n_cutUpComplete.length,
            startIndex = this.n_cutUpCompleteIndex % this.n_cutUpComplete.length;
        while (len > 0) {
            len--;
            let child = this.n_cutUpComplete[startIndex % this.n_cutUpComplete.length].children[2];
            if (child.active) {
                readyFood = child;
                this.n_cutUpCompleteIndex++;
                break;
            }
            startIndex++;
        }
        if (readyFood == null) return;
        _cook.n_move = true;

        let pos_1 = cc.v3(this.m_cooking.x + this.m_cooking.width / 2 + cook.width / 2, this.m_cooking.y + this.m_cooking.height / 3, 0);
        let pos_1_Mag = cook.position.sub(pos_1).mag();
        let pos_1_time = pos_1_Mag / this.n_cookMoveSpeed;

        let cutPos = cc.v3(this.m_cutNode.x + this.m_cutNode.width / 2 + cook.width / 2, this.m_cutNode.y);
        let cutMag = pos_1.sub(cutPos).mag();
        let cutTime = cutMag / this.n_cookMoveSpeed;
        let endPos = cc.v2(0, 0);

        if (_cook.n_info.type == 1) {
            endPos = cc.v2(this.m_cooking.x + 20, this.m_cooking.y - cook.height / 5 + 20);
        } else {
            endPos = cc.v2(this.m_cooking.x + cook.width / 2 + 50, this.m_cooking.y + 50);
        }
        cc.tween(cook)
            .to(pos_1_time, { x: pos_1.x, y: pos_1.y })
            .to(cutTime, { x: cutPos.x, y: cutPos.y })
            .call(() => {
                readyFood.active = false;
                cook.children[0].active = true;
            })
            .to(cutTime, { x: pos_1.x, y: pos_1.y })
            .to(pos_1_time, { x: endPos.x, y: endPos.y })
            .call(() => {
                _cook.n_move = false;
                if (!cooking.n_cooking && !config.gameOver) {
                    cook.children[0].active = false;
                    let info = _cook.n_info;
                    cooking.init(info, true);
                }
            })
            .start();
    }

    /**
     * 返回到开始界面
     */
    backToStartScene() {
        if (config.openPopup) return;
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        func.destroyBanner();
        // this.unschedule(this.counTDown);
        // this.m_startNode.getComponent(cc.Animation).stop();
        // this.recovery(true);
        this.unscheduleAllCallbacks();
        cc.director.loadScene("loading");
    }
    /**
     * 重开游戏
     */
    reopen(next: boolean) {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        // this.unschedule(this.counTDown);
        this.unscheduleAllCallbacks();
        // this.unschedule(this.startCreateMaterial);
        this.m_startNode.getComponent(cc.Animation).stop();
        this.recovery(next);
        this.init(next);
    }
    /**
     * 游戏结束回收
     */
    recovery(next: boolean) {
        for (let i = 0; i < this.m_material.childrenCount; i++) {
            let child = this.m_material.children[i];
            child.getComponent(Material).recoveryMaterial();
            i--;
        }
        this.n_orderNode.getComponent(Order).recoveryOrder();
        config.orderArr = [];
        config.orderCutUpTimeArr = [];
        config.selectOrder = null;
        if (next) {
            this.n_ready = [];
            this.n_cutUpComplete = [];
            for (let i = 0; i < this.m_readyAndComplete.childrenCount; i++) {
                let child = this.m_readyAndComplete.children[i];
                GameTools.gameTools.put(child, config.nodeToolPath[child.name]);
                i--;
            }
            for (let i = 0; i < this.m_delivery.childrenCount; i++) {
                let child = this.m_delivery.children[i];
                GameTools.gameTools.put(child, config.nodeToolPath[child.name]);
                i--;
            }
            // this.m_readyAndComplete.removeAllChildren();
            // this.m_delivery.removeAllChildren();
        } else {
            for (let i = 0; i < this.n_ready.length; i++) {
                let item = this.n_ready[i].children[1];
                if (item.childrenCount > 0) {
                    item.parent.getComponent(ReadyNode).state = "ready";
                    for (let j = 0; j < item.childrenCount; j++) {
                        let child = item.children[j];
                        child.getComponent(Material).recoveryMaterial();
                        i--;
                    }
                }
            }
            for (let i = 0; i < this.n_cutUpComplete.length; i++) {
                let item = this.n_cutUpComplete[i].children[2];
                if (item.active) {
                    item.active = false;
                }
            }
        }
    }
    /**
     * 暂停游戏
     */
    suspendGame(suspend: boolean = false) {
        if (!suspend) {
            this.schedule(this.counTDown, 1);
            this.m_startNode.getComponent(cc.Animation).play();
        } else {
            this.unschedule(this.counTDown);
            this.m_startNode.getComponent(cc.Animation).stop();
        }
    }
    /**
     * 点击暂停
     */
    async cliclSuspend() {
        if (config.openPopup) return;
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        config.openPopup = true;
        this.suspendGame(true);
        let suspend = this.n_view.getChildByName('suspend');
        if (suspend) {
            suspend.active = true;
            suspend.getComponent(Suspend).init();
        } else {
            let child: cc.Node = null;
            if (config.prefabArr[config.prefabPath.suspend]) {
                child = cc.instantiate(config.prefabArr[config.prefabPath.suspend]);
            } else {
                let prefab = await func.loadResouce(config.prefabPath.suspend, cc.Prefab) as cc.Prefab;
                config.prefabArr[config.prefabPath.suspend] = prefab;
                child = cc.instantiate(prefab);
            }
            child.parent = this.n_view;
            child.getComponent(Suspend).init();
        }
    }
    /**
     * 加载结算界面
     */
    async loadSettlementInterface(complete: boolean, reward: number) {
        if (config.openPopup) return;
        for (let i = 0; i < this.n_view.childrenCount; i++) {
            let child = this.n_view.children[i];
            child.active = false;
        }
        config.openPopup = true;
        let over = this.n_view.getChildByName('over');
        if (over) {
            over.active = true;
            over.getComponent(GameOver).init(complete, reward);
        } else {
            let child: cc.Node = null;
            if (config.prefabArr[config.prefabPath.over]) {
                child = cc.instantiate(config.prefabArr[config.prefabPath.over]);
            } else {
                let prefab = await func.loadResouce(config.prefabPath.over, cc.Prefab) as cc.Prefab;
                config.prefabArr[config.prefabPath.over] = prefab;
                child = cc.instantiate(prefab);
            }
            child.parent = this.n_view;
            child.zIndex = 1;
            child.getComponent(GameOver).init(complete, reward);
        }
    }
    /**加载vivo30s弹窗 */
    async loadVivoTime() {
        if (config.openPopup) return;
        config.openPopup = true;
        let timeNode = this.n_view.getChildByName("timeNode");
        if (timeNode) {
            timeNode.active = true;
        } else {
            if (!config.prefabArr[config.prefabPath.timeNode]) {
                let prefab = await func.loadResouce(config.prefabPath.timeNode, cc.Prefab) as cc.Prefab;
                config.prefabArr[config.prefabPath.timeNode] = prefab;
            }
            timeNode = cc.instantiate(config.prefabArr[config.prefabPath.timeNode]);
            timeNode.parent = this.n_view;
        }
        timeNode.getComponent(CutTime).init();
    }
    /**加载QQ弹窗 */
    async loadQQPopup(type: number) {
        // if (config.openPopup) return;
        config.openPopup = true;
        let mistake = this.n_view.getChildByName("touchByMistake");
        if (mistake) {
            mistake.active = true;
        } else {
            let prefab = await func.loadResouce("prefab/touchByMistake/touchByMistake", cc.Prefab) as cc.Prefab;
            mistake = cc.instantiate(prefab);
            mistake.parent = this.n_view;
        }
        mistake.zIndex = 2;
        mistake.getComponent(TouchByMistake).init(type);
    }
    lateUpdate() {
        this.m_money.string = config.money.toString();
    }
}
